;solution for euler 7

;algorithm described here: http://sandbox.mc.edu/~bennet/cs220/codeex/pgenintro.html
	


(defun isprime (n lowerprimes)
	(loop for prime in lowerprimes do
		(when (> (* prime prime) n) (return-from isprime t))
		(if (= (mod n prime) 0)
			(return-from isprime nil)))
	(return-from isprime t))


(defun prime-gen (num)
	(let ((primes (list 2)) (i 1) (count 1))
		(loop
			(when (= count num) (return))
			(setf i (+ i 2))
			(when (isprime i primes)
				(incf count)
				(setf (cdr (last primes)) (cons i nil))))
		(return-from prime-gen i)))
		
(defun solve-euler7 ()
	(format t "Euler 7: ~a" (prime-gen 10001)))
	
	
	;unfinished
;(defun isprime (n multiples primes)
;;	(loop for multiple in multiples
;		 for prime in primes do
;		(when (= multiple n)
;			(setf multipe (+ multiple prime))
;			(return-from isprime nil)))
;	(return-from isprime t))


;(defun prime-gen (num)
;	(let ((primes (list 2)) (multiples (list 4)) (i 3) (count 1))
;		(loop
;			(when (= count num) (return))
;			(setq i (+ i 1))
;			(when (isprime i multiples primes)
;				( i primes)
;				(push (* 2 i) multiples)
;				(incf count)))
;		(return-from prime-gen i)))




;(defun prime-gen2 (num)
;	(let ((primes (list 2)) (i 1) (count 1) (testresult))
;		(loop
;			(when (= count num) (return))
;			(setq i (+ i 2))
;			(setq testresult t)
;			(loop for prime in primes do
;				(if (= (mod i prime) 0)
;				(setq testresult nil)))
;			(when testresult
;				(incf count)
;				(when (= (mod count 100) 0) (print count))
;				(setf primes (append primes (list i)))))
;		(return-from prime-gen2 i)))
